package com.jz.yanshe.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jz.jd.rest.response.GoodsTabResponse;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.ui.widget.MyBanner;
import com.jz.yanshe.util.ImageLoaderHelper;
import com.jz.yanshe.util.PreferencesHelper;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.R;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.ConfigResponse;

/**
 * Created by Administrator on 2018/5/5.
 */

public class InitActivity extends BaseActivity {

    @BindView(R.id.banner)
    MyBanner banner;
    @BindView(R.id.btn_start)
    TextView btn_start;
    @BindView(R.id.img)
    ImageView img;

    int init;
    boolean flag;

    List<String> list_banner = new ArrayList<>();
    List<GoodsTabResponse.GoodsTab> list = new ArrayList<>();

    @OnClick(R.id.btn_start)
    void btn_start() {
        Bundle bundle = new Bundle();
        bundle.putSerializable("list", (Serializable) list);
        startActivity(HomeActivity.class,bundle);
        finish();
    }

    @Override
    protected void initViews() {
        btn_start.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initData() {
        init = PreferencesHelper.getInstance().getInt(this, "init", -1);
        getConfigList();
        getGoodsTab();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_init;
    }


    private void getConfigList() {
        RestProxy.getInstance().getConfigList(PreferencesHelper.getInstance().getString(this, "token"), "16", new Observer<ConfigResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ConfigResponse configResponse) {
                list_banner.clear();

                if(init  != -1){
                    banner.setVisibility(View.GONE);
                    btn_start.setVisibility(View.VISIBLE);
                    ImageLoaderHelper.getInstance().GlideImageLoader(InitActivity.this,configResponse.list.get(configResponse.list.size()-1).common_configuration_description,img);
                }


                for (ConfigResponse.Config banner : configResponse.list) {
                    list_banner.add(banner.common_configuration_description);
                }

                banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                            if(position == list_banner.size()-1){
                                btn_start.setVisibility(View.VISIBLE);
                                PreferencesHelper.getInstance().set(InitActivity.this,"init",1);
                                banner.setScanScroll(true);
                            }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                banner.setImages(list_banner).setBannerStyle(BannerConfig.NOT_INDICATOR)
                        .setBannerAnimation(Transformer.Tablet).setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        ImageLoaderHelper.getInstance().GlideImageLoader(context, (String) path, imageView);
                    }
                }).setDelayTime(100000).start();

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    private void getGoodsTab(){
        RestProxy.getInstance().getGoodsTab(PreferencesHelper.getInstance().getString(this, "token"), new Observer<GoodsTabResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(GoodsTabResponse goodsTabResponse) {
                list.clear();
                list.addAll(goodsTabResponse.list);
                flag = true;
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
